package com.tsing.cli.system.mapper;

import com.tsing.cli.system.entity.MenuEntity;
import com.tsing.cli.system.entity.UserEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author TheTsing
 * @since 2022-11-25 09:47
 */
public interface UserMapper extends BaseMapper<UserEntity> {

    @Select("""
            select distinct m.permissions
            from sys_role r
            inner join sys_user_role ur on r.id=ur.role_id
            inner join sys_role_menu rm on ur.role_id=rm.role_id
            inner join sys_menu m on rm.menu_id=m.id
            where ur.user_id=#{id} and r.status=1 and r.is_deleted=0 and m.permissions <>'' and m.status=1 and m.is_deleted=0
            """)
    Set<String> getPerms(@Param("id") String id);

    @Select("""
            select distinct m.id,m.pid,m.name,m.type,m.path,m.component,m.icon,m.weight
            from sys_menu m
            inner join sys_role_menu rm on m.id=rm.menu_id
            inner join sys_role r on rm.role_id=r.id
            inner join sys_user_role ur on r.id=ur.role_id
            where ur.user_id=#{id} and r.status=1 and r.is_deleted=0 and m.status=1 and m.is_deleted=0
            """)
    List<MenuEntity> build(@Param("id") String id);

}
